# configure-ntpd.pm
# Copyright (C) 2006  Stephane Alnet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 

#
# For more information: http://carrierclass.net/
#
use strict; use warnings;

package CCNCore::Provision::Application::ntpd;
use base qw(CCNCore::Provision::Base);

use CCN::Text;
use CCNCore::Scripting;

sub run()
{
    my $self = shift;
    return if $self->p->force_manager;

    $self->PACKAGE ('ntp-simple');

    $self->CHECKFOR ('/etc/init.d/ntp');
    $self->CHECKFOR ('/usr/sbin/ntpd');

    $self->enable('ntp');  # Debian
    $self->enable('ntpd'); # Redhat

    my $NTPNETWORKS = '';
    # SQLite does not offer IS TRUE
    $self->sql_do_for( <<'SQL', $self->p->server_pk,
        SELECT ipnet_ip, ipnet_mask FROM server_network WHERE server = ? AND ntp = 'TRUE'
SQL
    sub
    {
        my ($ip,$mask) = @_;
        $NTPNETWORKS .= "restrict $ip mask $mask nomodify notrap\n";
    });

    my $NTPMASTERS;
    my @servers = ();
    if( $self->{ntp_master} )
    {
        my $public_ntp = $self->sql_get( q(SELECT public_ntp FROM systems WHERE id = ?), $self->p->system_pk );

        if( not defined $public_ntp or $public_ntp eq '' )
        {
            $self->warn("You should define a set of public NTP servers for ".$self$self->p->system_name);
            @servers = qw( north-america.pool.ntp.org 0.pool.ntp.org 1.pool.ntp.org 2.pool.ntp.org ntp.nasa.gov pool.ntp.org );
        }
        else
        {   
            @servers = split(/[ ,]+/, $public_ntp );
        }
    }
    else
    {
        @servers = map { $self$self->p->make_server_name($_) } 
                       ($self->sql_get_array(q(SELECT id FROM server WHERE the_system = ? AND ntp_master = 'TRUE'), $self->p->system_pk));
    }
    $NTPMASTERS = join("\n",map { "server $_" } @servers);

    CAT '/etc/ntp.conf', <<"EOT";
restrict default nomodify notrap noquery
restrict 127.0.0.1

# -- CLIENT NETWORKS -------
${NTPNETWORKS}

# --- OUR TIMESERVERS -----
${NTPMASTERS}

# --- NTP MULTICASTCLIENT ---
#   multicastclient                        # listen on default 224.0.1.1
#       restrict 224.0.1.1 mask 255.255.255.255 nomodify notrap
#       restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

# --- GENERAL CONFIGURATION ---
server  127.127.1.0     # local clock
fudge   127.127.1.0 stratum 10

driftfile       /var/lib/ntp/drift
broadcastdelay  0.008
keys            /etc/ntp/keys
EOT

    IF_CHANGED '/etc/ntp.conf' => '/etc/init.d/ntp restart';

    #  stephane's lab has issues with time
    # CRONTAB q(12 * * * * ntpdate -u us.pool.ntp.org);
}
1;